﻿using Common.Contract.Authentication.Record;
using Common.Contract.Paging;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Service.Command;
using CrossSelling.Model.Interface;
using CrossSelling.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrossSelling.Core.ServiceCommand.Admin
{
    public class RetrieveChecklistHideDocByProductServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveChecklistHideDocRequest, RetrieveChecklistHideDocResponse>
    {
        #region Properties
        public UserRecord Dao;
        public string TextSearch;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public DateTime? FromDate;
        public int ProductId;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveChecklistHideDocByProductServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveChecklistHideDocRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.ProductId == 0)
            {
                throw new ArgumentException("Product must be set", "request");
            }
            else
            {
                ProductId = request.ProductId;
            }

            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
        }

        protected override bool OnExecute(RetrieveChecklistHideDocResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            try
            {
                List<ChecklistHideDocRecord> records = new List<ChecklistHideDocRecord>();

                List<ChecklistHideDocWhenAnswer> listCustomer = new List<ChecklistHideDocWhenAnswer>();
                listCustomer = Providers.ProductHandbooks.RetrieveListHideDocChecklist(out totalRecord, ProductId, Dao, TextSearch, Request.Lang, pageSize, pageIndex);
                Dictionary<int, ChecklistSubProduct> subProduct = Providers.ProductHandbooks.RetrieveSubProductWithoutSubId(ProductId).ToDictionary(x => x.SubProductId);
                int totalQuestion = 0;
                Dictionary<int, ChecklistQuestion> listQuestion = Providers.ProductHandbooks.RetrieveListQuestionChecklist(out totalQuestion, ProductId, Dao).ToDictionary(x => x.QuestionId);
                Dictionary<int, CheckListDocument> listDocument = Providers.ProductHandbooks.CheckListDocument(ProductId).ToDictionary(x => x.ChecklistDocId);
                if (listCustomer != null)
                {
                    listCustomer.ForEach(x => x.DocumentId.Split(',').ToList().ForEach(y =>
                    {
                        records.Add(new ChecklistHideDocRecord
                            {
                                SubProductName = Request.Lang.Equals("vi") ? subProduct[x.SubProductId].SubProductName : subProduct[x.SubProductId].SubProductNameEn,
                                Question = Request.Lang.Equals("vi") ? listQuestion[x.QuestionId].Title : listQuestion[x.QuestionId].TitleEn,
                                Answer = x.Answer,
                                Document = listDocument[int.Parse(y)].Document,
                                RuleHideId = x.DocHideId
                            });
                    }));
                }

                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = totalRecord
                };
            }
            catch (Exception e)
            {
                isSuccess = false;
                errorMessage = e.Message;

            }

            response.ErrorMessage = errorMessage;
            return isSuccess;
        }
    }
}
